package com.cherry.androidviewdemo.activity

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.cherry.androidviewdemo.R
import com.cherry.androidviewdemo.widget.WaterfallLayout
import kotlinx.android.synthetic.main.activity_layout.*
import java.util.*
import kotlin.math.abs


/**
 * @author DongMS
 * @since 2020/5/20
 */
class LayoutActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_layout)
    }

    fun addImg(view: View) {

        val random = Random()
        val num = abs(random.nextInt())
        val layoutParams = WaterfallLayout.WaterfallLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        val imageView = ImageView(this)
        when (num % IMG_COUNT) {
            0 -> imageView.setImageResource(R.drawable.p_1)
            1 -> imageView.setImageResource(R.drawable.p_2)
            2 -> imageView.setImageResource(R.drawable.p_3)
            3 -> imageView.setImageResource(R.drawable.p_4)
            4 -> imageView.setImageResource(R.drawable.p_5)
        }
//        imageView.setImageResource(R.drawable.withdraw_wx)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP

        waterfallLayout.addView(imageView, layoutParams)
    }

    companion object {
        private const val IMG_COUNT = 5
    }


}